package controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.event.AjaxBehaviorEvent;

import pojo.Hoadon;
import pojo.Loaisanpham;
import pojo.Sanpham;
import util.BeanUtil;
import util.MessageNotificationHepler;
import util.SearchVisitor;
import dao.HoadonDAO;
import dao.LoaisanphamDAO;
import dao.SanphamDAO;

@ManagedBean(name = "quanLyDonDatHangBean")
@ViewScoped
public class QuanLyDonDatHangBean implements Serializable {

	private List<Loaisanpham> lstLoaiSanPham = ((HeaderBean) BeanUtil
			.findBean("headerBean")).getLstLoaiSanPham();

	private static final long serialVersionUID = 1L;
	private List<Hoadon> lstHoaDon = new ArrayList<Hoadon>();
	private List<Sanpham> lstSanPham = new ArrayList<Sanpham>();
	private String chuoiTimKiem;

	private int maNhomLietKe; // có thể không xài
	private int maLoaiLietKe;
	private int trangThaiDuyet;
	private int trangThaiXoa;

	public void LietKeToanBo() {
		HoadonDAO hddao = new HoadonDAO();
		setLstHoaDon(hddao.getList());
		ClearLstSanPham();
	}

	public void LietKeTheoNhuCau() {
		System.out.println("Liet Ke Nhu Cau: " + getMaNhomLietKe() + " -- "
				+ getMaLoaiLietKe() + " -- " + getTrangThaiDuyet() + " -- "
				+ getTrangThaiXoa());

		HoadonDAO hddao = new HoadonDAO();

		if (-1 == getMaNhomLietKe())
			setLstHoaDon(hddao.GetHoaDonTheoMaLoaiVaTrangThai(
					getMaNhomLietKe(), getTrangThaiDuyet(), getTrangThaiXoa()));
		else
			setLstHoaDon(hddao.GetHoaDonTheoMaLoaiVaTrangThai(
					getMaLoaiLietKe(), getTrangThaiDuyet(), getTrangThaiXoa()));

		setLstHoaDon(hddao.removeDuplicate(getLstHoaDon()));
		// Chưa liệt kê:

		ClearLstSanPham();
	}

	public void changeNhomListener(AjaxBehaviorEvent event) {
		System.out.println("listener: called by "
				+ event.getComponent().getClass().getName());
		
		HtmlSelectOneMenu combobox = (HtmlSelectOneMenu) event.getComponent();
		int contentValue = (int) combobox.getValue();
		
		System.out.println(combobox.getId() + " -- " + contentValue);
		
		setMaNhomLietKe(contentValue);
		ThayDoiGiaTriLoaiSanPham();
	}

	public void ThayDoiGiaTriLoaiSanPham() {
		System.out.println("Thay doi gia tri Loai SP: " + getMaNhomLietKe());
		if (-1 == maNhomLietKe)
			lstLoaiSanPham = ((HeaderBean) BeanUtil.findBean("headerBean"))
					.getLstLoaiSanPham();
		else {
			lstLoaiSanPham = (new LoaisanphamDAO())
					.GetLoaiSanPhamTheoMaNhom(maNhomLietKe, false);
		}
	}

	public void TimKiemSanPham() {

		System.out.println("Tim Kiếm SP: ");
		SanphamDAO spdao = new SanphamDAO();
		SearchVisitor sv = new SearchVisitor();
		spdao.accept(sv, getChuoiTimKiem(), 0, -1);
		setLstSanPham(sv.getResultSanPham());
		System.out.println("Tim Kiếm SP KQ: " + getLstSanPham().size());
	}

	public void TimKiemHoaDon(Sanpham sp) {

		System.out.println("Tim Kiếm HD: " + sp.getTensanpham());
		HoadonDAO hddao = new HoadonDAO();
		lstHoaDon = hddao.GetHoaDon(sp);
		System.out.println("Tim Kiếm HD KQ: " + getLstHoaDon().size());

		// -----------
		ClearLstSanPham();
		getLstSanPham().add(sp);
	}

	public void Xoa(Hoadon hd) {
		System.out.println("Xóa: " + hd.getMahoadon());
		HoadonDAO hddao = new HoadonDAO();
		hd.setDaxoa(true);
		hddao.saveOrUpdate(hd);
		ClearLstHoaDon();

		
		MessageNotificationHepler.ShowMessage(String.format(
				"Đơn hàng với mã số '%s' đã được Xóa thành công!",
				hd.getMahoadon()));
	}
	public void HuyXoa(Hoadon hd) {
		System.out.println("Hủy Xóa: " + hd.getMahoadon());
		HoadonDAO hddao = new HoadonDAO();
		hd.setDaxoa(false);
		hddao.saveOrUpdate(hd);
		ClearLstHoaDon();
		
		MessageNotificationHepler.ShowMessage(String.format(
				"Đơn hàng với mã số '%s' đã được Phục hồi thành công!",
				hd.getMahoadon()));
	}
	public void Duyet(Hoadon hd) {
		System.out.println("Duyệt: " + hd.getMahoadon());
		HoadonDAO hddao = new HoadonDAO();
		hd.setDaduyet(true);
		hddao.saveOrUpdate(hd);
		ClearLstHoaDon();

		MessageNotificationHepler.ShowMessage(String.format(
				"Đơn hàng với mã số '%s' đã được Duyệt thành công!",
				hd.getMahoadon()));
	}
	public void HuyDuyet(Hoadon hd) {
		System.out.println("Hủy Duyệt: " + hd.getMahoadon());
		HoadonDAO hddao = new HoadonDAO();
		hd.setDaduyet(false);
		hddao.saveOrUpdate(hd);
		ClearLstHoaDon();

		MessageNotificationHepler.ShowMessage(String.format(
				"Đơn hàng với mã số '%s' đã được Hủy duyệt thành công!",
				hd.getMahoadon()));
	}

	private void ClearLstHoaDon() {
		getLstHoaDon().clear();
	}

	private void ClearLstSanPham() {
		getLstSanPham().clear();
	}

	// --------------------------
	public List<Hoadon> getLstHoaDon() {
		return lstHoaDon;
	}

	public void setLstHoaDon(List<Hoadon> lstHoaDon) {
		this.lstHoaDon = lstHoaDon;
	}

	public int getMaNhomLietKe() {
		return maNhomLietKe;
	}

	public void setMaNhomLietKe(int maNhomLietKe) {
		this.maNhomLietKe = maNhomLietKe;
	}

	public int getMaLoaiLietKe() {
		return maLoaiLietKe;
	}

	public void setMaLoaiLietKe(int maLoaiLietKe) {
		this.maLoaiLietKe = maLoaiLietKe;
	}

	public List<Loaisanpham> getLstLoaiSanPham() {
		return lstLoaiSanPham;
	}

	public void setLstLoaiSanPham(List<Loaisanpham> lstLoaiSanPham) {
		this.lstLoaiSanPham = lstLoaiSanPham;
	}

	public int getTrangThaiDuyet() {
		return trangThaiDuyet;
	}

	public void setTrangThaiDuyet(int trangThaiDuyet) {
		this.trangThaiDuyet = trangThaiDuyet;
	}

	public int getTrangThaiXoa() {
		return trangThaiXoa;
	}

	public void setTrangThaiXoa(int trangThaiXoa) {
		this.trangThaiXoa = trangThaiXoa;
	}

	public String getChuoiTimKiem() {
		return chuoiTimKiem;
	}

	public void setChuoiTimKiem(String chuoiTimKiem) {
		this.chuoiTimKiem = chuoiTimKiem;
	}

	public List<Sanpham> getLstSanPham() {
		return lstSanPham;
	}

	public void setLstSanPham(List<Sanpham> lstSanPham) {
		this.lstSanPham = lstSanPham;
	}
}
